﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;

public partial class Admin_Module_Category_Controls_CategoryList : System.Web.UI.UserControl
{
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
    private const int PageSize = 20;

    private string GridViewSortDirection
    {

        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = "ASC";
            return ViewState["sortDirection"].ToString();
        }
        set { ViewState["sortDirection"] = value; }

    }
    public int PageIndex
    {
        get
        {
            if (Request["page"] != null)
                return int.Parse(Request["page"].ToString());
            return 1;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDLL();
            BindData("");
        }

    }
    private void LoadDLL()
    {
        IList<TblCategory> ltc = new List<TblCategory>();
       // TblCategory tc = new TblCategoryRepository().SelectOneParentNull();
        //if (tc != null)
        //{
            HashSet<string> hs = new HashSet<string>();
            hs.Add("Name ASC");
            ltc = new TblCategoryRepository().SelectListForDDL(null, 1);
          //  ltc.Insert(0, tc);
            ddlParent.DataSource = ltc;
            ddlParent.DataTextField = "Name";
            ddlParent.DataValueField = "Id";
            ddlParent.DataBind();
        //}
    }
    private void BindData(string strSort)
    {
        HashSet<string> hs = new HashSet<string>();
        hs.Add(strSort);
        Guid? Parent = null;
        if (Request["ParentId"] != null && !Request["ParentId"].Equals(string.Empty))
            Parent = new Guid(Request["ParentId"].ToString());
        ddlParent.SelectedValue = Parent.HasValue ? Parent.Value.ToString() : "";
        int RowCount = 0;
        grv.DataSource = new TblCategoryRepository().SelectListItem(null, "", "", Parent, null,null, PageIndex, PageSize, out RowCount, hs);
        grv.DataBind();
        smartPager.PageSize = PageSize;
        smartPager.QueryStringKey = "page";
        smartPager.RowCount = RowCount;
        smartPager.UrlFormatString = "/Admin/Module/Category/CategoryList.aspx?ParentId=" + ddlParent.SelectedValue + "&page={0}";
        smartPager.Visible = RowCount > PageSize;

    }
    protected void grv_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (GridViewSortDirection.Equals("ASC"))
            GridViewSortDirection = "DESC";
        else
            GridViewSortDirection = "ASC";
        string strSort = e.SortExpression + " " + GridViewSortDirection;
        BindData(strSort);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/Module/Category/CategoryList.aspx?ParentId=" + ddlParent.SelectedValue);
    }
    protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("DeleteItem"))
        {
            HiddenField hdfId = (HiddenField)grv.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("hdfId");
            HiddenField hdfParentId = (HiddenField)grv.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("hdfParentId");
            string Whereclause = " And ParentId IS NULL ";
            if (!hdfParentId.Value.Equals(""))
            {
                Whereclause = " And ParentId='" + hdfParentId.Value + "'";
            }
            string Whereclause1 = " Id='" + hdfId.Value + "'";
            string Whereclause2 = "And Id<>'" + hdfId.Value + "'";
            UpdateOrder.UpdateOrders("TblCategories", Whereclause, Whereclause1, Whereclause2, 100);
            TblCategoryRepository.CreateInstant().Delete(new Guid(hdfId.Value));
            Response.Redirect(Request.RawUrl);
        }
    }
    protected void btnUpdateOrder_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < grv.Rows.Count; i++)
        {
            TextBox txtOrder = (TextBox)grv.Rows[i].FindControl("txtOrder");
            HiddenField hdfOrder = (HiddenField)grv.Rows[i].FindControl("hdfOrder");
            HiddenField hdfId = (HiddenField)grv.Rows[i].FindControl("hdfId");
            HiddenField hdfParentId = (HiddenField)grv.Rows[i].FindControl("hdfParentId");
            int OldIndex = int.Parse(hdfOrder.Value);
            int NewIndex = int.Parse(txtOrder.Text);

            string Whereclause = " And ParentId IS NULL ";
            if (!hdfParentId.Value.Equals(""))
            {
                Whereclause = " And ParentId='" + hdfParentId.Value + "'";
            }
            string Whereclause1 = " Id='" + hdfId.Value + "'";
            string Whereclause2 = " And Id<>'" + hdfId.Value + "'";
            if (OldIndex != NewIndex)
                UpdateOrder.UpdateOrders("TblCategories", Whereclause, Whereclause1, Whereclause2, NewIndex);
        }
        Response.Redirect(Request.RawUrl);
    }
}
